package slowtime

import (
	"time"
)

var timeSpace = int(time.Second)

var (
	now time.Time
)

func init() {
	go timeProc()
}

func Now() time.Time {
	return now
}

// 按照timeSpace更新时间，可减少运行time.Now()的次数，用于对时间不敏感的场景
func timeProc() {
	now = time.Now()
	offset := now.UnixNano() - now.Unix()*int64(time.Second)
	time.Sleep(time.Duration(offset)) // 整点更新

	for {
		now = time.Now()
		dif := now.UnixNano() - now.Unix()*int64(time.Second)
		time.Sleep(time.Duration(timeSpace - int(dif)))
	}
}
